﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Collections.Specialized;
using BSCMFF.Core.DataAccessLayer;


namespace BSCMFF.Core.MembershipDataProvider
{
    public class EntityMembershipDataProvider : MembershipProvider
    {
        public override void Initialize(string name, NameValueCollection config)
        {

            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (String.IsNullOrEmpty(name))
            {
                name = "EntityMembershipDataProvider";
            }

            //if (String.IsNullOrEmpty(config["description"]))
            //{
            //    config.Remove("description");
            //    config.Add("description", "Generic Database Provider");
            //}

            base.Initialize(name, config);

            //if (config["connectionStringName"] == null)
            //{
            //    // default to DataEngine
            //    config["connectionStringName"] = "DataSource";
            //}

            //connStringName = config["connectionStringName"];
            //config.Remove("connectionStringName");

            //// loading connection string
            //connString = ConfigurationManager.ConnectionStrings[connStringName].ConnectionString;

            //if (config["parmPrefix"] == null)
            //{
            //    // default
            //    config["parmPrefix"] = "@";
            //}

            //parmPrefix = config["parmPrefix"];
            //config.Remove("parmPrefix");

            //// loading database
            //string providerName = ConfigurationManager.ConnectionStrings[connStringName].ProviderName;
            //database = DbProviderFactories.GetFactory(providerName);

            //// creating connection when knowing the database
            //if (connection == null)
            //{
            //    connection = database.CreateConnection();
            //    connection.ConnectionString = connString;
            //}

            //// Throw an exception if unrecognized attributes remain
            //if (config.Count > 0)
            //{
            //    string attr = config.GetKey(0);
            //    if (!String.IsNullOrEmpty(attr))
            //    {
            //        throw new ProviderException("Unrecognized attribute: " + attr);
            //    }
            //}
        }

        private const int DefaultMinRequiredPasswordLength = 8;
        private const string EmailPattern = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";

        private string applicationName = "BSC MFF", connectionString;
        //private NameValueCollection configuration;

        public override string ApplicationName
        {
            get { return this.applicationName; }
            set { this.applicationName = value; }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public IList<User> MyGetAllUsers()
        {
            //throw new NotImplementedException();
            BSCEntities context = new BSCEntities();
            var users = (from u in context.Users
                         select u).ToList();

            return users;           
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            BSCEntities context = new BSCEntities();
            
            User validatedUser = (from u in context.Users
                                  where u.Login == username && u.Password == password
                                  select u).FirstOrDefault();
            return validatedUser == null ? false : true;
        }
    }
}
